package com.kayt.cyberBar.company.business.dao.impl;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.company.business.dao.ICompanyAccountDao;
import com.kayt.cyberBar.company.business.entity.po.BCompanyAccount;

@Repository("iCompanyAccountDao")
public class CompanyAccountDaoImpl implements ICompanyAccountDao {
	@Autowired
	private EntityDao entityDao;

	@Override
	public Long addAccount(BCompanyAccount account) throws SQLException {
		if (account != null) {
			return this.entityDao.save(account);
		}
		return 0L;
	}
	
	@Override
	public void updateAccount (BCompanyAccount account) throws SQLException {
		if (account != null) {
			this.entityDao.update(account);
		}
	}
	
	@Override
	public void batchInsert (List<BCompanyAccount> itemList) {
		this.entityDao.batchInsert(itemList);
	}
	
	@Override
	public int deleteAccount (BCompanyAccount account) {
		if (account != null) {
			this.entityDao.deleteDetached(account);
			return 0;
		}
		return -1;
	}
	
	@Override
	public BCompanyAccount getAccount (String accountId) {
		BCompanyAccount bCompanyAccount = null;
		if (accountId != null) {
			Long id;
			try {
				id = Long.parseLong(accountId);
			} catch (Exception e) {
				return null;
			}
			bCompanyAccount = this.entityDao.findById(id, BCompanyAccount.class);
		}
		return bCompanyAccount;
	}
	
	@Override
	public BCompanyAccount getAccountByCode (String code) throws SQLException {
		BCompanyAccount bCompanyAccount = null;
		if (code != null) {
			bCompanyAccount = this.entityDao.getObj(BCompanyAccount.class, " o.code=? ", new Object[]{code});
		}
		return bCompanyAccount;
	}
	
	@Override
	public PageData<BCompanyAccount> getAccountList (int pageNum, int pageSize, String hql, Object[] params, LinkedHashMap<String, String> accountMap) throws SQLException {
		return this.entityDao.getPagingData(BCompanyAccount.class, pageNum, pageSize, hql, params, accountMap);
	}
	
	@Override
	public List getAccountListByHql(int pageNum, int pageSize, String hql, Object[] params) throws SQLException {
		return this.entityDao.getPagingDataByHql(pageNum, pageSize, hql, params);
	}

	@Override
	public boolean checkValidate(String name) throws SQLException {
		if (StringUtils.isNotBlank(name)) {
			String hql = "select count(o.id) as count from BCompanyAccount o where o.cyberName=? ";
	        List countListMap = this.entityDao.getPagingDataByHql(-1, -1, hql, new Object[]{name});
	        if(countListMap != null && !countListMap.isEmpty()) {
	            Map countMap = (Map)countListMap.get(0);
	            Long tempCount = (Long)countMap.get("count");
	            if (tempCount.intValue() == 0) {
	            	return true;
	            }
	        }
		}
		return false;
	}
}
